# coding: UTF-8
"""
选择用于表示飞船的图像后，需要将其显示到屏幕上。
我们将创建一个名为ship的模块，其中包含Ship类，它负责管理飞船的大部分行为。
"""
import pygame
from pygame.sprite import Sprite


class Ship(Sprite):
    """【类】飞船
    """
    def __init__(self, FP_settings, FP_screen):
        """【类方法】初始化飞船并设置其初始位置
        """
        super(Ship, self).__init__()
        self.ATR_settings = FP_settings
        self.ATR_screen = FP_screen
        # 加载飞船图像并获取其外接矩形
        self.image = pygame.image.load(".\\images\\J-20_100x100px.png")
        self.rect = self.image.get_rect()
        self.ATR_screen_rect = self.ATR_screen.get_rect()
        # 将每艘新飞船放在屏幕底部中央
        self.rect.centerx = self.ATR_screen_rect.centerx  # 设置x轴方向居中
        self.rect.bottom = self.ATR_screen_rect.bottom    # 设置飞船在窗口底部
        # 移动标志
        self.ATR_moving_right = False
        self.ATR_moving_left = False
        self.ATR_moving_up = False
        self.ATR_moving_down = False
        # 在飞船的属性ATR_ship_center中存储浮点数
        self.ATR_ship_x = float(self.rect.centerx)
        self.ATR_ship_y = float(self.rect.bottom)

    def center_ship(self) -> None:
        """【类方法】重置飞船的位置到底部中央
        """
        self.ATR_ship_x = self.ATR_screen_rect.centerx  # 设置x轴方向居中
        self.ATR_ship_y = self.ATR_screen_rect.bottom  # 设置飞船在窗口底部
        return None

    def MET_blit_ship(self) -> None:
        """【类方法】在指定位置绘制飞船
        """
        self.ATR_screen.blit(self.image, self.rect)
        return None

    def MET_update(self) -> None:
        """【类方法】根据移动标志调整飞船位置
        """
        if (self.ATR_moving_right and self.rect.right < self.ATR_screen_rect.right):
            # self.ATR_ship_rect.centerx += 1
            self.ATR_ship_x += self.ATR_settings.ship_speed_factor  # 更新飞船的center值，而不是rect
        if (self.ATR_moving_left and self.rect.left > self.ATR_screen_rect.left):
            # self.ATR_ship_rect.centerx -= 1
            self.ATR_ship_x -= self.ATR_settings.ship_speed_factor  # 更新飞船的center值，而不是rect
        if (self.ATR_moving_up and self.rect.top > self.ATR_screen_rect.top):
            self.ATR_ship_y -= self.ATR_settings.ship_speed_factor
        if (self.ATR_moving_down and self.rect.bottom < self.ATR_screen_rect.bottom):
            self.ATR_ship_y += self.ATR_settings.ship_speed_factor
        # 根据self.ATR_ship_center更新rect对象，这里的浮点数（ATR_ship_center）转为了整型（rect.centerx）
        self.rect.centerx = self.ATR_ship_x  # 左右移动
        self.rect.bottom = self.ATR_ship_y  # 上下移动
        # print("飞船速度：", self.ATR_ship_center)
        # print("飞船位置：", self.ATR_ship_rect.centerx)
        return None
